#include <bits/stdc++.h>
#define int long long
#define ull unsigned long long
#define rg register
#define pc putchar
#define gc getchar
#define pf printf
#define space pc(' ')
#define enter pc('\n')
#define me(x,y) memset(x,y,sizeof(x))
#define pb push_back
#define FOR(i,k,t,p) for(rg int i(k) ; i <= t ; i += p)
#define ROF(i,k,t,p) for(rg int i(k) ; i >= t ; i -= p)
using namespace std ;
bool s_gnd ;
inline void read(){}
template<typename T,typename ...T_>
inline void read(T &x,T_&...p)
{
    x = 0 ;rg int f(0) ; rg char c(gc()) ;
    while(!isdigit(c)) f |= (c=='-'),c = gc() ;
    while(isdigit(c)) x = (x<<1)+(x<<3)+(c^48),c = gc() ;
    x = (f?-x:x) ;
    read(p...);
}
int stk[30],tp ;
inline void print(){}
template<typename T,typename ...T_>
inline void print(T x,T_...p)
{
    if(x < 0) pc('-'),x = -x ;
    do stk[++tp] = x%10,x /= 10 ; while(x) ;
    while(tp) pc(stk[tp--]^48) ; space ;
    print(p...) ;
}
bool S_GND ;
const int N = 505 ;
int n,m ;
int k[N],val[N] ;
double mx ;
string ans ;
ull h[N],rp[N],base[N] ;
char T[N],S[N],s[N][N] ;
void Check()
{
    double res = 1,tot = 0,chk = 0 ;
    FOR(i,1,n,1) rp[i] = rp[i-1]*31+S[i] ;
    FOR(i,1,m,1)
    {
        FOR(j,k[i],n,1) if(h[i] == rp[j]-rp[j-k[i]]*base[k[i]])
            res *= val[i],++tot,chk = 1 ;
    }
    double num = pow(res,1.0/tot) ;
    if(num > mx && chk)
    {
        mx = num,ans = "" ;// cout<<mx<<"\n" ;
        FOR(i,1,n,1) ans += S[i] ;
    }
}
void Dfs(int x)
{
    if(x == n+1) {Check() ; return ;}
    if(T[x] != '.') S[x] = T[x],Dfs(x+1) ;
    else FOR(i,0,9,1) S[x] = i+'0',Dfs(x+1) ;
}
void Solve()
{
    int chk = 0 ;
    FOR(p,1,m,1)
    {
        FOR(i,1,n-k[i]+1,1)
        {
            int top = 0,pd = 0 ;
            FOR(j,i,i+k[i]-1,1) if(s[p][++top] != T[j])
                if(T[j] != '.') {pd = 1 ; break ;} top = 0 ;
            if(!pd) FOR(j,i,i+k[i]-1,1) T[j] = s[p][++top] ;
            if(!pd) {chk = 1 ; break ;}
        }
        if(chk) break ;
    }
    FOR(i,1,n,1) if(T[i] == '.') T[i] = '0' ;
    FOR(i,1,n,1) ans += T[i] ;
}
signed main()
{
//cerr<<(double)(&s_gnd-&S_GND)/1024.0/1024.0 ;
	freopen("magic.in","r",stdin) ;
	freopen("magic.out","w",stdout) ;
    read(n,m),scanf("%s",T+1),base[0] = 1 ;
    FOR(i,1,n,1) base[i] = base[i-1]*31 ;
    FOR(i,1,m,1) 
    {
        scanf("%s",s[i]+1),k[i] = strlen(s[i]+1) ;
        FOR(j,1,k[i],1) h[i] = h[i]*31+s[i][j] ; read(val[i]) ;
    }
    if(n <= 6) Dfs(1) ;
    else Solve() ;
    cout<<ans ;
    return 0 ;
}